package com.spelet.simulation;

import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.CatmullRomSpline;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.spelet.StaticSprites;

public class Bee {
	private static boolean[] flipped;
	private Sprite spriteToDraw;

	private int animTimer;
	private float moveTimer;
	private int animStep;
	private float movement;

	private Array<Vector3> flowers;
	private Vector3 startflower;
	private Vector3 target;
	public Vector3 position;
	private int pollinate;

	private CatmullRomSpline splines;
	private List<Vector3> points;
	private Vector3 startpoint;
	private Vector3 point1;
	private Vector3 point2;
	private Vector3 point3;
	private Vector3 endpoint;
	private Vector3 beeHive;
	private int nrpoints;
	private int currentpoint;

	private Vector3 direction;
	private Vector3 distancetoNext;
	public int currentlyAtRow;
	public int tileX, tileY, spriteX, spriteY;
	private Sprite cross;
	private Sprite beehive;
	int width;
	int height;
	public Bee(Map map, int z, int y) {
		tileX = z;
		tileY = y;
		movement = 0;
		moveTimer=0;
		startflower = new Vector3();
		target = new Vector3();
		position = new Vector3();
		direction= new Vector3();
		distancetoNext = new Vector3();
		beeHive = new Vector3();

		width = Gdx.graphics.getWidth();
		height = Gdx.graphics.getHeight();
		startpoint = new Vector3();
		point1 = new Vector3();
		point2 = new Vector3();
		point3 = new Vector3();
		endpoint = new Vector3();
		nrpoints = 50;
		currentpoint =0;
		beehive = StaticSprites.beehive;
		beeHive.set(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()-15),0);
		beehive.setPosition(beeHive.x,beeHive.y);

		splines = new CatmullRomSpline();
		splines.add(startpoint);
		splines.add(startpoint);
		splines.add(point1);
		splines.add(point2);
		splines.add(point3);
		splines.add(endpoint);
		splines.add(endpoint);

		spriteX = z*64;
		spriteY = y*64;
		animStep = 0;

		cross = new Sprite(new Texture(Gdx.files.internal("data/cross.png")));
		spriteToDraw = StaticSprites.beeAnimation[animStep];
		if (flipped == null)
			flipped = new boolean[7];
		flowers = new Array<Vector3>();

		for (int x = map.blocks[0].length-1; x >= 0; x--)
			for (int u = 0; u < map.blocks.length; u++) {
				if(map.terrainBlocks[u][x].hasFlower){
					addFlower(map.terrainBlocks[u][x].fxPos, map.terrainBlocks[u][x].fyPos);
				}
			}
		firstTarget();

	}
	public void render(SpriteBatch sb, float r, float b, float g, float a) {
		if (direction.x > 0) {
			if (!flipped[animStep]) {
				flipped[animStep] = true;
				spriteToDraw.flip(true,false);
			}
		} else {
			if (flipped[animStep]) {
				flipped[animStep] = false;
				spriteToDraw.flip(true,false);
			}
		}
		spriteToDraw.setSize(32,32);
		spriteToDraw.setColor(r,g,b,a);
		spriteToDraw.setPosition(spriteX, spriteY);
		spriteToDraw.draw(sb);
	}

	public void renderhive(SpriteBatch sb, float r, float b, float g, float a){
		beehive.setColor(r,g,b,a);
		beehive.setPosition(beeHive.x, beeHive.y);
		beehive.draw(sb);
	}

	public void resetBee(Map map){
		flowers.clear();
		for (int x = map.blocks[0].length-1; x >= 0; x--)
			for (int u = 0; u < map.blocks.length; u++) {
				if(map.terrainBlocks[u][x].hasFlower){
					addFlower(map.terrainBlocks[u][x].fxPos, map.terrainBlocks[u][x].fyPos);
				}
			}
		firstTarget();
	}

	public void setControlPoints(Vector3 start, Vector3 end){
		startpoint.set(start);

		if(start.x >= width/2){
			point1.set(MathUtils.random(width/2, width),MathUtils.random(0, height),0);
			if(MathUtils.random(0, 5) == 0)			
				point2.set(beeHive);
			else
				point2.set(flowers.get(MathUtils.random(flowers.size-1)));
			if(end.x >= width/2)
				point3.set(MathUtils.random(width/2, width),MathUtils.random(height/2, height),0);
			else
				point3.set(MathUtils.random(0,width/2),MathUtils.random(0,height/2),0);		
		}
		else{
			point1.set(MathUtils.random(width/2, width),MathUtils.random(0, height),0);
			if(MathUtils.random(0, 5) == 0)			
				point2.set(beeHive);
			else
				point2.set(flowers.get(MathUtils.random(flowers.size-1)));
			if(end.x >= width/2)
				point3.set(MathUtils.random(width/2, width),MathUtils.random(height/2, height),0);	
			else
				point3.set(MathUtils.random(0,width/2),MathUtils.random(0,height/2),0);
		}

		endpoint.set(end);
		if(points!= null)
			points.clear();
		points = splines.getPath(nrpoints);
	}
	public void firstTarget(){
		if(flowers.size > 0){
			startflower.set(flowers.get(MathUtils.random(flowers.size-1)));
			position.set(startflower);
			target.set(flowers.get(MathUtils.random(flowers.size-1)));

			setControlPoints(startflower,target);

			distancetoNext.set(points.get(currentpoint));
			distancetoNext.x -= startflower.x;
			distancetoNext.y -= startflower.y;

			direction.set(points.get(currentpoint));
			direction.x -= startflower.x;
			direction.y -= startflower.y;
			direction.nor();
		}
	}

	public void update(Simulation sim) {
		pollinate -= Gdx.graphics.getDeltaTime()*1000;
		if(pollinate <= 0){
			moveTimer = Gdx.graphics.getDeltaTime();
			animTimer += Gdx.graphics.getDeltaTime()*1000;
			int step = 40;
			if (animTimer <= step) {
				animStep = 0;
				spriteToDraw = StaticSprites.beeAnimation[animStep];
			}else if (animTimer <= step*2){
				animStep =1;
				spriteToDraw = StaticSprites.beeAnimation[animStep];
			} else if (animTimer <= step*3){
				animStep =2;
				spriteToDraw = StaticSprites.beeAnimation[animStep];
			} else if (animTimer <= step*4){
				animStep=3;
				spriteToDraw = StaticSprites.beeAnimation[animStep];
			} else if (animTimer <= step*5){
				animStep =4;
				spriteToDraw = StaticSprites.beeAnimation[animStep];
			}else if (animTimer <= step*6){
				animStep =5;
				spriteToDraw = StaticSprites.beeAnimation[animStep];
			}else {
				animStep = 6;
				spriteToDraw = StaticSprites.beeAnimation[animStep];
				animTimer =0;
			}
		}

		movement = moveTimer*200;
		while(movement > 0){
			if (direction.len()*movement >= distancetoNext.len()) {	
				if(currentpoint >= points.size()-1){
					pollinate = 1000;
					startflower.set(target);
					position.set(startflower);
					target.set(flowers.get(MathUtils.random(flowers.size-1)));
					currentpoint=0;
					setControlPoints(startflower,target);
					animStep = 0;

				}
				else{

					animStep = 0;
					spriteToDraw = StaticSprites.beeAnimation[animStep];
					moveTimer =0;
					startflower.set(points.get(currentpoint));
					position.set(startflower);
					currentpoint++;
					target.set(points.get(currentpoint));

					distancetoNext.set(target);
					distancetoNext.x -= startflower.x;
					distancetoNext.y -= startflower.y;

					direction.set(target);
					direction.x -= startflower.x;
					direction.y -= startflower.y;
					direction.nor();

					movement = 0;
				}
			}
			else {			
				position.x += movement*direction.x;
				position.y += movement*direction.y;

				distancetoNext.x -= movement*direction.x;
				distancetoNext.y -= movement*direction.y;					
				movement = 0;
			}		
		}
		spriteX = (int)position.x;
		spriteY = (int)position.y;
		currentlyAtRow = spriteY/64;
	}

	public void addFlower(int x,int y){
		flowers.add(new Vector3(x,y,0));
	}

}


